#include "data_pool.h"


data_pool::data_pool(int _capacity)
	: capacity(_capacity)
	, pool(_capacity)
{
	int consume_step = 0;
	int product_step = 0;
	sem_init(&space_sem, 0, _capacity);	//第二个参数为0表示在线程间共享
	sem_init(&message_sem, 0, 0);
}


void data_pool::get_message(std::string& out_message)
{
	int consume_step = 0;

	sem_wait(&message_sem);

	out_message = pool[consume_step];

	sem_post(&space_sem);

	consume_step++;
	consume_step %= capacity;
}

void data_pool::put_message(const std::string& in_message)
{
	int product_step = 0;

	sem_wait(&space_sem);

	pool[product_step] = in_message;

	sem_post(&message_sem);

	product_step++;
	product_step %= capacity;
}


data_pool::~data_pool()
{
	sem_destroy(&space_sem);
	sem_destroy(&message_sem);
}

